<?php
/**
 * 《注塑需求》实现类
 *
 * @author 孙晓晔
 * @version $Id: Test.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/ZsUtil.class.php');

class Requirement extends ZsUtil {
	public $tbl = Constant::tbl_zs_requirement;

	public $urgent = array(
		'-' => '请选择',
		'1' => '紧急',
		'2' => '重要',
		'3' => '普通',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '等待安排',
		'3' => '已经安排',
		'4' => '无机安排',

		'5' => '暂停安排',
		'6' => '取消安排',

		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'mold' => '产品',
		'urgent' => '紧急程度',	
		'amount' => '订单数量',
		't1' => '开始时间',

		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'emp' => '员工',
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('urgent_option', $this->urgent);
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-7 day'));

		$controller->display($request, 'zs/requirement/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'urgent') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $this->urgent[$row['urgent']];
		}
		
		if ($px == 't1') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['t1'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$type_s = $request['type_s'];
		$urgent_s = $request['urgent_s'];

		$date1_s = $request['date1_s'];

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		if (!empty($name_s)) {
			$where .= " AND (name LIKE '%" . $name_s . "%' OR content LIKE '%" . $name_s . "%')";
		}

		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND type = '" . $type_s . "'";
		}

		if (!empty($urgent_s) && $urgent_s <> '-') {
			$where .= " AND urgent = '" . $urgent_s . "'";
		}

		if (!empty($date1_s)) {
			$where .= " AND DATE_FORMAT(t1, '%Y-%m-%d') <= '" . $date1_s . "' AND DATE_FORMAT(t2, '%Y-%m-%d') >= '" . $date1_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}
	
	function _my() {
		return '';
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'zs/requirement/list');
		return true;
	}

	function _plan($id) {
		$row = Common::id(Constant::tbl_zs_plan, $id);
		if (is_array($row)) {
			return sprintf("%s/%s", Name::machine($row['machine']), Name::emp($row['worker']));
		}

		return '';
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['status'] > 2) {
			return false;
		}

		if ($this->_audit('40')) {
			return true;
		}

		if ($row['emp'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$customer = $this->id(Constant::tbl_customer, $id);

		$customer['country'] = $this->name_code(Constant::tbl_country, $customer['country']);
		$controller->assign_by_ref('customer', $customer);

		$controller->assign('t1', strtotime('1 day'));

		$controller->display($request, 'zs/requirement/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$mold = $this->_input($request['mold']);
		$type = $request['type'];
		$urgent = $request['urgent'];
						
		$cavity = $request['cavity'];
		$period = $request['period'];
		$weight1 = $request['weight1'];
		$weight2 = $request['weight2'];
		$material = $request['material'];
		$color = $request['color'];
		$amount = $request['amount'];

		$t1 = $request['t1'];
		$t2 = $request['t2'];

		$content = addslashes($request['content']);
		$emp = $this->_input($request['emp']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(mold, type, urgent, cavity, period, weight1, weight2, material, color, amount, t1, t2, content, emp, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $mold, $type, $urgent, $cavity, $period, $weight1, $weight2, $material, $color, $amount, $t1, $t2, $content, $emp, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'zs/requirement/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$mold = $this->_input($request['mold']);
		$type = $request['type'];
		$urgent = $request['urgent'];		
						
		$cavity = $request['cavity'];
		$period = $request['period'];
		$weight1 = $request['weight1'];
		$weight2 = $request['weight2'];
		$material = $request['material'];
		$color = $request['color'];
		$amount = $request['amount'];

		$t1 = $request['t1'];
		$t2 = $request['t2'];

		// $content = sprintf("%s\n%s 修改", addslashes($request['content']), Name::emp($_SESSION['id']));
		$content = addslashes($request['content']);
		$emp = $this->_input($request['emp']);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET mold = '%s', type = '%s', urgent = '%s', cavity = '%s', period = '%s', weight1 = '%s', weight2 = '%s',";
		$fmt .= " material = '%s', color = '%s', amount = '%s', t1 = '%s', t2 = '%s', content = '%s', emp = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $mold, $type, $urgent, $cavity, $period, $weight1, $weight2, $material, $color, $amount, $t1, $t2, $content, $emp, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'zs/requirement/copy');
		return true;
	}

	/**
	 * 审核
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function verify($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'zs/requirement/verify');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function verifyPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		// $content = sprintf("%s\n%s 审核", addslashes($request['content']), Name::emp($_SESSION['id']));
		$content = addslashes($request['content']);
		
		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);		

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'zs/requirement/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['mold_t'] = Name::mold($row['mold']);
		$row['type_t'] = $this->type[$row['type']];
		$row['urgent_t'] = $this->urgent[$row['urgent']];
		
		$row['plan_t'] = $this->_plan($row['plan']);
		// $row['date'] = $this->_date($row['t1'], $row['t2']);
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$row = $this->id($this->tbl, $id);
			
			if ($this->_is_delete($row)) {
				$fmt = "DELETE FROM %s WHERE id = '%s'";
				$sql = sprintf($fmt, $this->tbl, $id);
				// debug($sql);
				$db->query($sql);
			}
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function reason($controller, $request) {
		$this->pretreat($controller);

		$status = $request['status'];
		$controller->assign_by_ref('reason', $this->status[$status]);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$customer = $this->id(Constant::tbl_customer, $row['customer']);

		$customer['country'] = $this->name_code(Constant::tbl_country, $customer['country']);
		$controller->assign_by_ref('customer', $customer);

		$controller->display($request, 'zs/requirement/reason');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function reasonPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		// $reason = sprintf("%s\n%s 操作", addslashes($request['reason']), Name::emp($_SESSION['id']));
		$content = addslashes($request['content']);

		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET reason = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $reason, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function status_continue($controller, $request) {
		$this->pretreat($controller);

		return $this->status_restart($controller, $request);
	}

	function status_restart($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$row = $this->id($this->tbl, $id);
		if (empty($row['plan'])) {
			$status = 2;
		} else {
			$status = 3;
		}

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $status, $id);
		// debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 * 无机安排
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function nomachine($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'zs/requirement/nomachine');
		return true;
	}

	/**
	 * 无车可派
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function nomachinePost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$reason = sprintf("%s\n%s 操作", addslashes($request['reason']), Name::emp($_SESSION['id']));

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET reason = '%s', status = '4', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $reason, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function plan($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		require_once(dirname(__FILE__) . '/Plan.class.php');
		$plan = new Plan();
		$data = $plan->_list();
		
		$controller->assign_by_ref('plan_option', $data);

		$controller->display($request, 'zs/requirement/plan');
		return true;
	}

	/**
	 * 用车安排
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function planPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$plan = $this->_input($request['plan']);
		$content = sprintf("\n%s\n%s 派车", addslashes($request['content']), Name::emp($_SESSION['id']));

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET plan = '%s', content = concat(content, '%s'), status = '3', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $plan, $content, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$this->_notice_2($id);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 * 直接派车
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function machine($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'zs/requirement/machine');
		return true;
	}
	
	function machinePost($controller, $request) {
		$id = $request['id'];
		
		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);
		
		$machine = $this->_input($request['machine']);
		$worker = $this->_input($request['worker']);
		$content = addslashes($request['content']);
		
		require_once(dirname(__FILE__) . '/Plan.class.php');
		$aa = new Plan();
		
		$plan = $aa->_machine($row, $machine, $worker, $content);
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET plan = '%s', status = '3', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, Constant::tbl_zs_requirement, $plan, $id);
		// debug($sql);
		$db->query($sql);
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function group($controller, $request) {
		$this->pretreat_group($controller);
	
		// $controller->assign('t1', strtotime('-1 month'));
		// $controller->assign('t2', strtotime('+1 day'));
	
		$controller->display($request, 'zs/requirement/group');
		return true;
	}
	
	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');
	
		$controller->assign('groupby', $this->groupby[$px]);
	
		return $this->groupby_dd($px);
	}
	
	function groupby_dd($px) {
		if ($px == '1') {
			return "DATE_FORMAT(t1, '%Y-%m-%d %H')";
		}
	
		if ($px == '2') {
			return "DATE_FORMAT(t1, '%Y-%m-%d')";
		}
	
		if ($px == '3') {
			return "DATE_FORMAT(t1, '%Y-%m')";
		}
	
		return $px;
	}
	
	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} else {
	
			$row = $this->_name_t($row, $px);
		}
	
		return $row;
	}

	function t($controller, $request) {
		$this->pretreat($controller);

		echo substr("0755-89809672", 5);

		logdate($controller, $request);
		return true;
	}
	
	function view_mold($controller, $request) {
		$id = $request['id'];
	
		$ret = $this->_list_mold($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'zs/requirement/_list_c');
		return true;
	}
}
?>